﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Model.Models.BBS;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK.Service.BBS
{
    public class ShareFileTypeService : BaseService, IShareFileTypeService
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IShareFileTypeRepository _shareFileTypeRepo;
        public ShareFileTypeService(IMapper mapper, IShareFileTypeRepository shareFileTypeRepo, IUnitOfWork unitOfWork) : base(mapper)
        {
            _shareFileTypeRepo = shareFileTypeRepo;
            this.unitOfWork = unitOfWork;
        }

        public bool SubmitShareFileType(ShareFileTypeInfo shareFileType, out string errMsg)
        {
            errMsg = "";
            if (shareFileType.Id > 0)
            {
                _shareFileTypeRepo.UpdEntity(shareFileType);
            }
            else
            {
                _shareFileTypeRepo.AddEntity(shareFileType);
            }
            return unitOfWork.SaveChangesEntity();
        }
        public ShareFileTypeInfo? GetShareFileInfo(long shareFileId)
        {
            return _shareFileTypeRepo.QueryEntityById(shareFileId);
        }
        public bool DelShareFileType(long shareFileTypeId, out string errmsg)
        {
            errmsg = "";
            var shareFileType = _shareFileTypeRepo.QueryEntityById(shareFileTypeId);
            if (shareFileType == null)
            {
                errmsg = "参数错误";
            }
            _shareFileTypeRepo.DelEntity(shareFileType);
            return unitOfWork.SaveChangesEntity();
        }

        public List<ShareFileTypeInfo> GetShareFileTypePageData(string? typeName, out int totalCount, int pageIndex = 1, int pageSize = 20)
        {
            var query = _shareFileTypeRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(typeName))
            {
                query = query.Where(t => t.TypeName == typeName);
            }
            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }
    }
}
